package com.example.demo;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.nio.Buffer;

public class MainActivity extends AppCompatActivity {
    private Button btnInsertStu;
    private Button btnInsertSco;
    private Button btnDelStu;
    private Button btnUpdateStu;
    private Button btnQueryStu;
    private Button btnQuerySco;
    private Button btnDelSco;
    private Button btnUpdateSco;
    private TextView txtResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnInsertStu=findViewById(R.id.btn_insert_stu);
        btnInsertStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri= Uri.parse("content://com.gdcp.student/student");
                ContentValues values =new ContentValues();
                values.put("stuno","171001");
                values.put("name","zhangsan");
                values.put("age",19);
                getContentResolver().insert(uri,values);
            }
        });

        txtResult=findViewById(R.id.txt_result);
        btnQueryStu=findViewById(R.id.btn_query_stu);
        btnQueryStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri= Uri.parse("content://com.gdcp.student/student");
                Cursor cursor = getContentResolver().query(uri,null,null,null,null);
                if (cursor!=null&&cursor.getCount()>0){
                    StringBuffer buffer=new StringBuffer();
                    for (cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                        String stuno = cursor.getString(0);
                        String name = cursor.getString(1);
                        int age = cursor.getInt(2);
                        buffer.append(stuno).append("   ").append(name).append("  ").append(age).append("\n");
                    }
                    txtResult.setText(buffer);
                }else {
                    txtResult.setText("没有数据");
                }

            }
        });

        btnDelStu=findViewById(R.id.btn_dle_stu);
        btnDelStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                String where="stuno='171001'";
                int i = getContentResolver().delete(uri,where,null);
                Toast.makeText(MainActivity.this,"学生表删除了"+i+"行",Toast.LENGTH_SHORT).show();
            }
        });
        btnUpdateStu=findViewById(R.id.btn_update_stu);
        btnUpdateStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                String where = "age=19";
                ContentValues values= new ContentValues();
                values.put("age",28);
                int i= getContentResolver().update(uri,values,where,null);
                Toast.makeText(MainActivity.this,"学生表更新了"+i+"行",Toast.LENGTH_SHORT).show();
            }
        });

        btnInsertSco=findViewById(R.id.btn_insert_sco);
        btnInsertSco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri= Uri.parse("content://com.gdcp.student/score");
                ContentValues values =new ContentValues();
                values.put("stuno","171001");
                values.put("english",89);
                values.put("android",98);
                getContentResolver().insert(uri,values);
            }
        });

        btnQuerySco=findViewById(R.id.btn_query_sco);
        btnQuerySco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri= Uri.parse("content://com.gdcp.student/score");
                Cursor cursor = getContentResolver().query(uri,null,null,null,null);
                if (cursor!=null&&cursor.getCount()>0){
                    StringBuffer buffer=new StringBuffer();
                    for (cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                        String stuno = cursor.getString(0);
                        int english = cursor.getInt(1);
                        int android = cursor.getInt(2);
                        buffer.append(stuno).append("   ").append(english).append("  ").append(android).append("\n");
                    }
                    txtResult.setText(buffer);
                }else {
                    txtResult.setText("没有数据");
                }

            }
        });

        btnDelSco=findViewById(R.id.btn_dle_sco);
        btnDelSco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://com.gdcp.student/score");
                String where="stuno='171001'";
                int i = getContentResolver().delete(uri,where,null);
                Toast.makeText(MainActivity.this,"分数表删除了"+i+"行",Toast.LENGTH_SHORT).show();
            }
        });
        btnUpdateSco=findViewById(R.id.btn_update_sco);
        btnUpdateSco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://com.gdcp.student/score");
                String where = "english=89";
                ContentValues values= new ContentValues();
                values.put("english",60);
                int i = getContentResolver().update(uri,values,where,null);
                Toast.makeText(MainActivity.this,"分数表更新了"+i+"行",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
